package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.ObjUtil;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseFinancingInfo;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseFinancingInfoVo;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseInformationVo;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseFinancingInfoService;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseInformationService;
import com.ruoyi.manage.organization.service.IOcOrderCreditService;
import com.ruoyi.manage.organization.service.IOcOrderLoanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 企业融资需求Controller
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@Api(tags = "企业融资需求控制器")
@RestController
@RequestMapping("/manage/demand")
public class EcEnterpriseFinancingInfoController extends BaseController {
    /**
     * 配置表别名 默认空
     */
    private final static String TABLE_PREFIX = StrUtil.initTableAlias(null);
    @Autowired
    private IEcEnterpriseFinancingInfoService ecEnterpriseFinancingInfoService;

    @Autowired
    private IEcEnterpriseInformationService ecEnterpriseInformationService;

    @Autowired
    private IOcOrderCreditService iOcOrderCreditService;

    @Autowired
    private IOcOrderLoanService iOcOrderLoanService;


    /**
     * 查询企业融资需求列表
     */
    @ApiOperation(value = "查询企业融资需求", notes = "企业融资需求管理")
    @PreAuthorize("@ss.hasPermi('manage:demand:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcEnterpriseFinancingInfoVo ecEnterpriseFinancingInfo, QueryWrapper<EcEnterpriseFinancingInfo> queryWrapper) {
        setQueryCondition(ecEnterpriseFinancingInfo, queryWrapper);
        startPage();
        List<EcEnterpriseFinancingInfo> list = ecEnterpriseFinancingInfoService.list(queryWrapper);
        return getDataTable(list);
    }

    private void setQueryCondition(EcEnterpriseFinancingInfoVo ecEnterpriseFinancingInfo, QueryWrapper<EcEnterpriseFinancingInfo> queryWrapper) {
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getCreditCodeDelete())) {
            queryWrapper.eq(TABLE_PREFIX + "credit_code_delete", ecEnterpriseFinancingInfo.getCreditCodeDelete());
        }
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getApplyState())) {
            queryWrapper.eq(TABLE_PREFIX + "apply_state", ecEnterpriseFinancingInfo.getApplyState());
        }
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getProfitState())) {
            queryWrapper.eq(TABLE_PREFIX + "profit_state", ecEnterpriseFinancingInfo.getProfitState());
        }
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getApplyNo())) {
            queryWrapper.eq(TABLE_PREFIX + "apply_no", ecEnterpriseFinancingInfo.getApplyNo());
        }
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getProvinceName())) {
            queryWrapper.like(TABLE_PREFIX + "province_name", ecEnterpriseFinancingInfo.getProvinceName());
        }
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getCityName())) {
            queryWrapper.like(TABLE_PREFIX + "city_name", ecEnterpriseFinancingInfo.getCityName());
        }
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getAreaName())) {
            queryWrapper.like(TABLE_PREFIX + "area_name", ecEnterpriseFinancingInfo.getAreaName());
        }
        if (ObjUtil.isNotNull(ecEnterpriseFinancingInfo.getBeginCreateTime()) && ObjUtil.isNotNull(ecEnterpriseFinancingInfo.getEndCreateTime())) {
            queryWrapper.between(TABLE_PREFIX + "create_time", DateUtil.beginOfDay(ecEnterpriseFinancingInfo.getBeginCreateTime()), DateUtil.endOfDay(ecEnterpriseFinancingInfo.getEndCreateTime()));
        }
        if (StrUtil.isNotBlank(ecEnterpriseFinancingInfo.getEnterpriseName())) {
            queryWrapper.like(TABLE_PREFIX + "enterprise_name", ecEnterpriseFinancingInfo.getEnterpriseName());
        }

        queryWrapper.orderByDesc(TABLE_PREFIX + "create_time");
    }

    //    取消融资需求
    @PutMapping("/cancelFinance/{id}")
    public AjaxResult cancelFinance(@PathVariable("id") Long id) {
        ecEnterpriseFinancingInfoService.lambdaUpdate().eq(EcEnterpriseFinancingInfo::getId, id).set(EcEnterpriseFinancingInfo::getApplyState, "4").update();
        return AjaxResult.success();
    }

    @GetMapping("/financeInfoList/{id}")
    public AjaxResult financeInfoList(@PathVariable("id") Long id) {
        List<EcEnterpriseFinancingInfo> list = ecEnterpriseFinancingInfoService.selectFinanceInfoService(id);
        return AjaxResult.success(list);
    }

    /**
     * 导出企业融资需求列表
     */
    @PreAuthorize("@ss.hasPermi('manage:demand:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcEnterpriseFinancingInfoVo ecEnterpriseFinancingInfo, QueryWrapper<EcEnterpriseFinancingInfo> queryWrapper) {
        if (CollUtil.isEmpty(ecEnterpriseFinancingInfo.getIds())) {
            setQueryCondition(ecEnterpriseFinancingInfo, queryWrapper);
        } else {
            queryWrapper.in(TABLE_PREFIX + "id", ecEnterpriseFinancingInfo.getIds());
        }
        List<EcEnterpriseFinancingInfo> list = ecEnterpriseFinancingInfoService.list(queryWrapper);
        ExcelUtil<EcEnterpriseFinancingInfo> util = new ExcelUtil<EcEnterpriseFinancingInfo>(EcEnterpriseFinancingInfo.class);
        util.exportExcel(response, list, "demand");
    }

    /**
     * 获取企业融资需求详细信息
     */
    @ApiOperation(value = "获取企业融资需求", notes = "企业融资需求管理")
//    @PreAuthorize("@ss.hasPermi('manage:demand:query')" )
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ecEnterpriseFinancingInfoService.queryGetById(id));
    }


    @GetMapping("/getfinancingInfo")
    public AjaxResult getfinancingInfo(String appId) {
        HashMap<String, Object> map = new HashMap<>();
        EcEnterpriseFinancingInfo ecEnterpriseFinancingInfo = ecEnterpriseFinancingInfoService.getOne(new QueryWrapper<EcEnterpriseFinancingInfo>().eq("apply_no", appId));
        map.put("financingInfo", ecEnterpriseFinancingInfo);
        EcEnterpriseInformationVo ecEnterpriseInfo = ecEnterpriseInformationService.queryMoreGetById(ecEnterpriseFinancingInfo.getEnterpriseId());
        map.put("enterpriseInfo", ecEnterpriseInfo);
        return AjaxResult.success(map);
    }

    /**
     * 新增企业融资需求
     */
    @ApiOperation(value = "新增企业融资需求", notes = "企业融资需求管理")
    @PreAuthorize("@ss.hasPermi('manage:demand:add')")
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcEnterpriseFinancingInfo ecEnterpriseFinancingInfo) {
        return toAjax(ecEnterpriseFinancingInfoService.addEcEnterpriseFinancingInfo(ecEnterpriseFinancingInfo) ? 1 : 0);
    }


    @PostMapping("/addFinancingInfo")
    @RepeatSubmit
    public AjaxResult addFinancingInfo(@Validated @RequestBody EcEnterpriseFinancingInfoVo ecEnterpriseFinancingInfo) {
        return toAjax(ecEnterpriseFinancingInfoService.addEcEnterpriseFinancingInfoMore(ecEnterpriseFinancingInfo) ? 1 : 0);
    }

    /**
     * 修改企业融资需求
     */
    @ApiOperation(value = "修改企业融资需求", notes = "企业融资需求管理")
    @PreAuthorize("@ss.hasPermi('manage:demand:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody EcEnterpriseFinancingInfo ecEnterpriseFinancingInfo) {
        return toAjax(ecEnterpriseFinancingInfoService.updateEcEnterpriseFinancingInfo(ecEnterpriseFinancingInfo) ? 1 : 0);
    }

    /**
     * 删除企业融资需求
     */
    @ApiOperation(value = "删除企业融资需求", notes = "企业融资需求管理")
    @PreAuthorize("@ss.hasPermi('manage:demand:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ecEnterpriseFinancingInfoService.deleteEcEnterpriseFinancingInfoByIds(ids) ? 1 : 0);
    }
}
